/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.SolutionFace;
import com.chlitina.store.modules.expert.entity.SolutionFaceFood;
import com.chlitina.store.modules.expert.service.ProductService;
import com.chlitina.store.modules.expert.service.SolutionFaceFoodService;
import com.chlitina.store.modules.expert.service.SolutionFaceService;

/**
 * 方案健康食品Controller
 * @author liujiang
 * @version 2014-02-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/solutionFaceFood")
public class SolutionFaceFoodController extends BaseController {

	@Autowired
	private SolutionFaceFoodService solutionFaceFoodService;
	
	@Autowired
	private SolutionFaceService solutionFaceService;
	
	@Autowired
	private ProductService productService;
	
	@ModelAttribute
	public SolutionFaceFood get(@RequestParam(required=false) Long sid,
			@RequestParam(required=false) Long pid) {
		if (sid != null && pid != null){
			return solutionFaceFoodService.getOneByPK(sid, pid); 
		}else{
			return new SolutionFaceFood();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Long solutionId, SolutionFaceFood solutionFaceFood, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (solutionId != null) {
			Page<SolutionFaceFood> page = solutionFaceFoodService.find(new Page<SolutionFaceFood>(request, response), solutionId); 
			model.addAttribute("page", page);
			model.addAttribute("solutionFaceId", solutionId);
		}
		return "modules/expert/solutionFaceFoodList";
	}

	@RequestMapping(value = "listDetail")
	public String listDetail(Long solutionId, SolutionFaceFood solutionFaceFood, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (solutionId != null) {
			Page<SolutionFaceFood> page = solutionFaceFoodService.find(new Page<SolutionFaceFood>(request, response), solutionId); 
			model.addAttribute("solutionFace", solutionFaceService.get(solutionId));
			model.addAttribute("page", page);
		}
		return "modules/expert/solutionFaceFoodListDetail";
	}

	@RequestMapping(value = "form")
	public String form(SolutionFaceFood solutionFaceFood, Model model, Long solutionId, Long productId) {
		if (solutionId != null && productId == null) {
			SolutionFace solutionFace = solutionFaceService.get(solutionId);
			solutionFaceFood.setSolutionFace(solutionFace);
		} else {
			solutionFaceFood = solutionFaceFoodService.getOneByPK(solutionId, productId);
		}
		Map<Integer, Integer> orderMap = new HashMap<Integer, Integer>();
		for(int i = 1; i < 16; i++) {
			orderMap.put(i, i);
		}
		model.addAttribute("solutionFaceFood", solutionFaceFood);
		model.addAttribute("orderMap", orderMap);
		return "modules/expert/solutionFaceFoodForm";
	}
	
	@RequestMapping(value = "save")
	public String save(SolutionFaceFood solutionFaceFood, Model model,  HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes, Long solutionId, Long productId) {
		String ids = request.getParameter("productIds");
		if (ids != null && !"".equals(ids)) {
			String[] productIds = ids.split(",");
			for (String id : productIds) {
				if (StringUtils.isNotEmpty(id)) {
					Long productIdLong = Long.parseLong(id);
					Product product = productService.get(productIdLong);
					solutionFaceFood.setProduct(product);
					if (!beanValidator(model, solutionFaceFood)){
						return form(solutionFaceFood, model, solutionId, productId);
					}
					solutionFaceFoodService.save(solutionFaceFood);
				}
			}
		}
		String oldProductId = request.getParameter("oldProductId");
		if (StringUtils.isNotEmpty(oldProductId)) {
			Long oldProductIdLong = Long.parseLong(oldProductId);
			if (!oldProductIdLong.equals(solutionFaceFood.getProduct().getId())) {
				SolutionFaceFood old = solutionFaceFoodService.getOneByPK(solutionFaceFood.getSolutionFace().getId(), oldProductIdLong);
				solutionFaceFoodService.delete(old);
			}
		}
		Long solutionFaceId = solutionFaceFood.getSolutionFace().getId();
		addMessage(redirectAttributes, solutionFaceService.get(solutionFaceId).getName() + "的'" + solutionFaceFood.getProduct().getName() + "'保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceFood/list?solutionId=" + solutionFaceId;
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long solutionId, Long productId, RedirectAttributes redirectAttributes) {
		SolutionFaceFood solutionFaceFood = solutionFaceFoodService.getOneByPK(solutionId, productId);
		SolutionFace solutionFace = null;
		if (solutionId != null && productId != null) {
			solutionFace = solutionFaceService.get(solutionId);
			solutionFaceFood.setSolutionFace(solutionFace);
			Product product = productService.get(productId);
			solutionFaceFood.setProduct(product);
		} else {
			solutionFace = solutionFaceService.get(solutionFaceFood.getSolutionFace().getId());
		}
		solutionFaceFoodService.delete(solutionFaceFood);
		addMessage(redirectAttributes, solutionFaceFood.getSolutionFace().getName()+"的'" + solutionFaceFood.getProduct().getName() + "'删除成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceFood/list?solutionId=" + solutionFace.getId();
	}

}
